﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Documents;
using System.IO;
using System.IO.Packaging;
using System.Windows.Xps.Packaging;
using System.Windows.Xps.Serialization;
using System.Xml;
using System.Windows.Markup;

namespace Infrastructure.WpfClient.Services.Documents
{
    public class DocumentService :IDocumentService
    {

        public void GenerateXpsFromFlowDocument(string Document, string target)
        {
            var xaml = Document;
            var textreader = new System.IO.StringReader(xaml);
            var reader = XmlReader.Create(textreader);
            var flowDocument = (FlowDocument)XamlReader.Load(reader);
            GenerateXps(flowDocument, target);
        }

        public void GenerateXps(FlowDocument Document, string target)
        {
          

            DocumentPaginator paginator = ((IDocumentPaginatorSource)Document).DocumentPaginator;

            using (Package container = Package.Open(target,  FileMode.Create))
            {
                using (XpsDocument  xpsDoc = new XpsDocument( container, CompressionOption.Maximum ))
                {
                    XpsSerializationManager xsr = new XpsSerializationManager(new XpsPackagingPolicy(xpsDoc), false);
                    xsr.SaveAsXaml(paginator);
                }                
            }
        }
    }
}
